package com.wyc.controller;

import com.wyc.pojo.HouseholdInformationSheet;
import com.wyc.service.HouseholdInformationSheetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.sql.Timestamp;
import java.time.Instant;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName HouseholdInformationSheet
 * @Author 王韫琛
 * @Date 2021/1/18 14:15
 * @Version 1.0
 */
@Controller
public class HouseholdInformationSheetController {
    //controller调service层
    @Autowired
    private HouseholdInformationSheetService householdInformationSheetService;
    //普通管理员跳转到添加住户信息页面
    @RequestMapping("/user_add_information")
    public String user_add_information(){
        return "/view/admin_jsp/user_add_information";
    }
    //普通管理员跳转到住户信息维护界面
    @RequestMapping("/household_information_maintenance")
    public String household_information_maintenance(){
        return "/view/admin_jsp/household_information_maintenance";
    }
    //普通管理员添加住户信息操作
    @RequestMapping("/adminToAddUserInformation")
    @ResponseBody
    public String adminAddUserInformation(HouseholdInformationSheet householdInformationSheet){
        householdInformationSheet.setBirthday(Timestamp.from(Instant.now()).toString());
        Map<String, Object> map = new HashMap<>();
        if (householdInformationSheetService.addHouseInformation(householdInformationSheet)>0){
            map.put("success",true);
            map.put("message","添加成功");
        }else {
            map.put("success",false);
            map.put("message","修改失败");
        }
        return cn.hutool.json.JSONUtil.toJsonStr(map);
    }
    //普通管理员检查用户身份证是否存在
    @RequestMapping("/findByCertificateNumber")
    @ResponseBody
    public String findByCertificateNumber(String certificate_number){
        Map<String,Object> map = new HashMap<>();
        if (householdInformationSheetService.findByCertificateNumber(certificate_number)!=null){
            map.put("success",true);
            map.put("message","身份证已存在,请重新输入");
        }else {
            map.put("success",false);
            map.put("message","可以使用");
        }
        return cn.hutool.json.JSONUtil.toJsonStr(map);
    }
    //普通管理员检查手机号是否存在
    @RequestMapping("/findByCellPhoneNumber")
    @ResponseBody
    public String findByCellPhoneNumber(String cell_phone_number){
        Map<String,Object> map = new HashMap<>();
        if (householdInformationSheetService.findByCellPhoneNumber(cell_phone_number)!=null){
            map.put("success",true);
            map.put("message","手机号已存在,请重新输入");
        }else {
            map.put("success",false);
            map.put("message","可以使用");
        }
        return cn.hutool.json.JSONUtil.toJsonStr(map);
    }
    //普通管理员分页显示住户信息
    @ResponseBody
    @RequestMapping("/adminFindPage")
    public String adminFindPage(@RequestParam(defaultValue = "1") Integer pageNum,
                           @RequestParam(defaultValue = "10") Integer pageSize){
        Map<String,Object> map = new HashMap<>();
        //调用service得到分页对象
        List<HouseholdInformationSheet> pageInfo = householdInformationSheetService.findPage(pageNum,pageSize);
        map.put("code",0);
        map.put("msg","");
        map.put("count",householdInformationSheetService.getTotal());//总数量
        map.put("data",pageInfo);//数据列表
        return cn.hutool.json.JSONUtil.toJsonStr(map);
    }
    //管理员根据身份证查询用户信息
    @RequestMapping("/selectByCertificateNumber")
    @ResponseBody
    public String selectByCertificateNumber(String certificate_number){
        System.out.println(certificate_number);
        Map<String,Object> map = new HashMap<>();
        List<HouseholdInformationSheet> pageInfo = householdInformationSheetService.selectByCertificateNumber(certificate_number);
        map.put("code",0);
        map.put("msg","");
        map.put("count",1);//总数量
        map.put("data",pageInfo);
        return cn.hutool.json.JSONUtil.toJsonStr(map);
    }
    //管理员根据id删除用户信息
    @RequestMapping("/deleteHouseholdInformationSheetById")
    @ResponseBody
    public String deleteHouseholdInformationSheetById(Integer id){
        System.out.println(id);
        Map<String,Object> map = new HashMap<String,Object>();
        if (householdInformationSheetService.deleteHouseholdInformationSheetById(id)>0){
            map.put("success",true);
            map.put("message","删除成功");
        }else {
            map.put("success",false);
            map.put("message","删除失败");
        }
        return cn.hutool.json.JSONUtil.toJsonStr(map);
    }
    //更新用户信息
    @RequestMapping("/updateHouseholdInformationSheet")
    @ResponseBody
    public String updateHouseholdInformationSheet(HouseholdInformationSheet householdInformationSheet){
        System.out.println(householdInformationSheet.toString());
        householdInformationSheet.setBirthday(Timestamp.from(Instant.now()).toString());
        Map<String, Object> map = new HashMap<>();
        if (householdInformationSheetService.updateHouseholdInformationSheet(householdInformationSheet)>0){
            map.put("success",true);
            map.put("message","修改成功");
        }else {
            map.put("success",false);
            map.put("message","修改失败");
        }
        return cn.hutool.json.JSONUtil.toJsonStr(map);
    }
}
